﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Sinacor.Infra.Tools.ServiceRegistration.Web
{
    public partial class Types : Page, IServiceController
    {
        private ServiceController _controller;
        private const string DeleteMessage = "return confirm('Deseja realmente excluir este item?');";
        private const string InsertMessage = "alert('Este item já existe!');";

        protected void Page_Load(object sender, EventArgs e)
        {
            GetServiceControllerInstance();

            if (!IsPostBack)
            {
                if (Request.QueryString["TypeId"] == null)
                    SearchType();
                else
                    SearchParentType();
            }
        }
        
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["TypeId"] != null)
                SearchParentType();
            else
                SearchType();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["TypeId"] != null)
            {
                if (string.IsNullOrEmpty(txtTypeIdEdit.Value))
                    InsertParentType();
                else
                    UpdateParentType();
            }
            else
            {
                if (string.IsNullOrEmpty(txtTypeIdEdit.Value))
                    InsertType();
                else
                    UpdateType();
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            ClearTypeFields();
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            txtNameSpace.Text = string.Empty;
            txtType.Text = string.Empty;

            SearchType();
        }

        protected void gdvType_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (Request.QueryString["TypeId"] != null)
            {
                DeleteParentType(e.RowIndex);
            }
            else
            {
                DeleteType(e.RowIndex);
            }
            ClearTypeFields();
            ((IServiceController)this).ShowTypeList();
        }

        protected void gdvType_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "if (this.className != 'rowgridclick') this.className = 'rowgridhouver';");
                e.Row.Attributes.Add("onmouseout", "if (this.className != 'rowgridclick') this.className = 'rowgrid';");
                e.Row.Attributes.Add("onclick", "SetItem(this, '5');");
                ((ImageButton)e.Row.Cells[e.Row.Cells.Count - 1].FindControl("btnDelete")).Attributes.Add("OnClick", DeleteMessage);

                if (e.Row.DataItem != null && ((Entity.Type)e.Row.DataItem).TypeId != null)
                {
                    var list = from n in _controller.TypeList
                               where n.ParentTypeId.Equals(((Entity.Type)e.Row.DataItem).TypeId)
                               select n;

                    if (list.ToArray<Entity.Type>().Length > 0)
                    {
                        ((ImageButton)e.Row.FindControl("imgSearchParentType")).Visible = true;
                        ((ImageButton)e.Row.FindControl("imgSearchParentType")).Attributes.Add("onclick", "openwindowfilter('" + ((Entity.Type)e.Row.DataItem).TypeId + "')");

                    }
                    else
                    {
                        ((ImageButton)e.Row.FindControl("imgSearchParentType")).Visible = false;
                    }
                }
            }
        }

        protected void gdvType_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gdvType.PageIndex = e.NewPageIndex;
            ((IServiceController)this).ShowTypeList();
        }

        private void GetServiceControllerInstance()
        {
            if (Session["SessionServiceController"] != null)
            {
                _controller = (ServiceController)Session["SessionServiceController"];
                _controller.View = this;
            }
            else
            {
                _controller = new ServiceController(this);
                Session["SessionServiceController"] = _controller;
            }
        }

        private void SearchType()
        {
            _controller.TypeSearch = new Entity.Type();
            _controller.TypeSearch.Namespace = txtNameSpace.Text;
            _controller.TypeSearch.TypeName = txtType.Text;
            _controller.SelectTypeList();
        }

        private void SearchParentType()
        {
            _controller.TypeSearch = new Entity.Type();
            _controller.TypeSearch.Namespace = txtNameSpace.Text;
            _controller.TypeSearch.TypeName = txtType.Text;
            //_controller.SelectParentTypeList(Convert.ToInt32(Request.QueryString["TypeId"]));
        }

        private void InsertType()
        {
            _controller.TypeEdit = new Entity.Type();
            _controller.TypeEdit.Namespace = txtNameSpaceEdit.Text;
            _controller.TypeEdit.TypeName = txtTTypeEdit.Text;

            _controller.InsetType();

            ClearTypeFields();
        }

        private void InsertParentType()
        {
            _controller.TypeEdit = new Entity.Type();
            _controller.TypeEdit.ParentTypeId = Convert.ToInt32(Request.QueryString["TypeId"]);
            _controller.TypeEdit.Namespace = txtNameSpaceEdit.Text;
            _controller.TypeEdit.TypeName = txtTTypeEdit.Text;

            //_controller.InsertParentType();

            ClearTypeFields();
        }

        private void UpdateType()
        {
            _controller.TypeEdit = new Entity.Type();
            _controller.TypeEdit.TypeId = Convert.ToInt32(txtTypeIdEdit.Value);
            _controller.TypeEdit.Namespace = txtNameSpaceEdit.Text;
            _controller.TypeEdit.TypeName = txtTTypeEdit.Text;
            
            _controller.UpdateType();

            ClearTypeFields();
        }

        private void UpdateParentType()
        {
            _controller.TypeEdit = new Entity.Type();
            _controller.TypeEdit.TypeId = Convert.ToInt32(txtTypeIdEdit.Value);
            _controller.TypeEdit.ParentTypeId = Convert.ToInt32(Request.QueryString["TypeId"]);
            _controller.TypeEdit.Namespace = txtNameSpaceEdit.Text;
            _controller.TypeEdit.TypeName = txtTTypeEdit.Text;

            //_controller.UpdateParentType();

            ClearTypeFields();
        }

        private void ClearTypeFields()
        {
            txtNameSpaceEdit.Text = string.Empty;
            txtTypeIdEdit.Value = string.Empty;
            txtTTypeEdit.Text = string.Empty;
        }

        private void DeleteType(int index)
        {
            _controller.TypeEdit = new Entity.Type();
            _controller.TypeEdit.TypeId = Convert.ToInt32(gdvType.DataKeys[index].Value);

            _controller.DeleteType();
        }

        private void DeleteParentType(int index)
        {
            _controller.TypeEdit = new Entity.Type();
            _controller.TypeEdit.TypeId = Convert.ToInt32(gdvType.DataKeys[index].Value);

           // _controller.DeleteParentType();
        }

        #region IServiceController Members

        void IServiceController.ShowTreeView()
        {
            throw new NotImplementedException();
        }

        void IServiceController.ShowModule()
        {
            throw new NotImplementedException();
        }

        void IServiceController.ShowSubModule()
        {
            throw new NotImplementedException();
        }

        void IServiceController.ShowModuleList()
        {
            throw new NotImplementedException();
        }

        void IServiceController.ShowSubModuleList()
        {
            throw new NotImplementedException();
        }

        void IServiceController.ShowSubModulePort()
        {
            throw new NotImplementedException();
        }

        void IServiceController.ShowServiceList()
        {
            throw new NotImplementedException();
        }

        void IServiceController.ShowMethodList()
        {
            throw new NotImplementedException();
        }

        void IServiceController.ShowParameterList()
        {
            throw new NotImplementedException();
        }

        void IServiceController.ShowTypeList()
        {
            //if (Request.QueryString["TypeId"] != null)
            //    gdvType.DataSource = _controller.ParentTypeList;
            //else
            //    gdvType.DataSource = _controller.TypeList;

            //gdvType.DataBind();
        }

        void IServiceController.ShowAttributeList()
        {
            
        }
               
        void IServiceController.ShowMessageError()
        {
            ScriptManager.RegisterStartupScript(Page, ScriptManagerType.GetType(), "Alert", InsertMessage, true);
        }

        void IServiceController.ShowProtocol() //inc - esertorio - 200810311655
        {
            throw new NotImplementedException();
        }

        #endregion

    }
}
